<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Hasil_seleksi extends CI_Controller {
	
	function __construct(){
		parent::__construct();
		$this->load->model('mdl_hasil_seleksi');
		//$this->load->model('mdl_fakultas');
		//$this->load->model('mdl_prodi');
	}
	
	function index(){
		$this->load->view('hasil_seleksi');
	}
	
	
	function grid(){
		$data = $this->mdl_hasil_seleksi->getdata();
		echo $this->mdl_hasil_seleksi->togrid($data['row_data'], $data['row_count']);
	}
	
	
	function add(){
		// $data['kode'] = '';
		// $data['thn_penerimaan'] = '';
		// $data['id_pendaftaran'] = '';
		// $data['nama'] = '';
		// $data['alamat'] = '';
		// $data['asal_sekolah'] = '';
		// $data['fakultas_1'] = '';
		// $data['prodi_1'] = '';
		// $data['fakultas_2'] = '';
		// $data['prodi_2'] = '';
		// $data['nama_ortu'] = '';
		// $data['alamat_ortu'] = '';
		// $data['pekerjaan'] = '';
		$this->load->view('hasil_seleksi_form');
	}
	
	/*
	function edit($kode){
		$r = $this->mdl_hasil_seleksi->getdataedit($kode);
		
		$data['kode'] = $kode;
		$data['thn_penerimaan'] = $r->row()->thn_penerimaan;
		$data['id_pendaftaran'] = $r->row()->id_pendaftaran;
		$data['nama'] = $r->row()->nama;
		$data['alamat'] = $r->row()->alamat;
		$data['asal_sekolah'] = $r->row()->asal_sekolah;
		$data['fakultas_1'] = $r->row()->fakultas_1;
		$data['prodi_1'] = $r->row()->prodi_1;
		$data['fakultas_2'] = $r->row()->fakultas_2;
		$data['prodi_2'] = $r->row()->prodi_2;
		$data['nama_ortu'] = $r->row()->nama_ortu;
		$data['alamat_ortu'] = $r->row()->alamat_ortu;
		$data['pekerjaan'] = $r->row()->pekerjaan;
		$this->load->view('prodi_form', $data);
	}
	
	
	function save($aksi){
		# init
		$status = "";
		$result = false;
		$data['pesan_error'] = '';
		
		# get post data
		foreach($_POST as $key => $value){
			$data[$key] = $value;
		}
		
		# rules validasi form
		$this->form_validation->set_rules("thn_penerimaan", 'Tahun Penerimaan', 'trim|required|xss_clean');
		$this->form_validation->set_rules("id_pendaftaran", 'ID Pendaftaran', 'trim|required|xss_clean');
		$this->form_validation->set_rules("nama", 'Nama', 'trim|required|xss_clean');
		$this->form_validation->set_rules("alamat", 'Alamat', 'trim|required|xss_clean');
		$this->form_validation->set_rules("asal_sekolah", 'Asal sekolah', 'trim|required|xss_clean');
		$this->form_validation->set_rules("fakultas_1", 'Fakultas 1', 'trim|required|xss_clean');
		$this->form_validation->set_rules("prodi_1", 'Prodi 1', 'trim|required|xss_clean');
		$this->form_validation->set_rules("fakultas_2", 'Fakultas 1', 'trim|required|xss_clean');
		$this->form_validation->set_rules("prodi_2", 'Prodi 2', 'trim|required|xss_clean');
		$this->form_validation->set_rules("nama_org_tua", 'Nama Orang Tua', 'trim|required|xss_clean');
		$this->form_validation->set_rules("alamat_org_tua", 'Alamat Orang Tua', 'trim|required|xss_clean');
		$this->form_validation->set_rules("pekerjaan", 'Pekerjaan', 'trim|required|xss_clean');
		
		# message rules
		$this->form_validation->set_message('required', 'Field %s harus diisi.');

		$data['pesan_error'] = '';
		if ($this->form_validation->run() == FALSE){
			//$data["pesan_error"] .= trim(form_error('kd_prodi',' ',' '))==''?'':form_error('kd_prodi',' ',' ').'<br>';
			$data["pesan_error"] .= trim(validation_errors(' ',' '))==''?'':validation_errors(' ',' ');
		}else{
			if($aksi=="add"){ // add
				$result = !$this->mdl_hasil_seleksi->isExistKode($data['kd_prodi']);
				if ($result){
					$result = $this->mdl_hasil_seleksi->InsertOnDb($data);
				}else{
					$data['pesan_error'] .= 'Sudah terdapat kode dalam database!';
				}
			}else { // edit
				if($data['kode'] == $data['kd_prodi']){ // jika kode tidak diubah
					$result=$this->mdl_hasil_seleksi->UpdateOnDb($data);
				}else{
					$result = !$this->mdl_hasil_seleksi->isExistKode($data['kd_prodi']);
					if ($result){
						$result=$this->mdl_hasil_seleksi->UpdateOnDb($data);
					}else{
						$data['pesan_error'] .= 'Sudah terdapat kode dalam database!';
					}
				}
			}
		}
		
		if($result){
			echo json_encode(array('success'=>true));
		}else{
			echo json_encode(array('msg'=>$data['pesan_error']));
		}
	}
	
	function delete($kode){
		$result = $this->mdl_hasil_seleksi->DeleteOnDb($kode);
		if ($result){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'Data gagal dihapus'));
		}
	}  */
	
}